package cn.edu.ecnu.gis.client.alert.view;

import cn.edu.ecnu.gis.client.alert.MessengerServiceClientImpl;
import cn.edu.ecnu.gis.client.alert.model.Contact;
import cn.edu.ecnu.gis.client.alert.model.ContactList;
import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.GadgetClass;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowCloseListener;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DeckPanel;

public class MessengerView extends Gadget implements WindowCloseListener, WindowResizeListener, HistoryListener {

    private DeckPanel mainPanel = new DeckPanel();
    private ContactListView contactListView;
    private SignInView signIn;
    private ContactList contactList;
    private List openChatWindows = new ArrayList();
    private Map allChatWindows = new HashMap();
    private MessengerViewListener listener;

    public static class Class extends GadgetClass {

        public Class() {
            super("GBIM-基于位置的即时通讯", false);
        }

        public Gadget newGadget() {
            String url = GWT.getModuleBaseURL();
            MessengerServiceClientImpl messengerService =
                    new MessengerServiceClientImpl(url + "messengerservice");
            MessengerView mv = messengerService.getView();
            mv.setGadgetClass(this);
            return mv;

        // return new MessengerView(this);
        }
    }

    public void setMessengerViewListener(MessengerViewListener listener) {
        this.listener = listener;

    }

    public MessengerView(MessengerViewListener listener) {

        initWidget(mainPanel);
        this.listener = listener;

        signIn = new SignInView(this);

        mainPanel.add(signIn);
        mainPanel.showWidget(0);
        Window.addWindowCloseListener(this);
        Window.addWindowResizeListener(this);
        History.addHistoryListener(this);
    }

    private MessengerView(Class aThis) {
        super(aThis);
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public MessengerViewListener getListener() {
        return listener;
    }

    public ContactListView getContactListView() {
        return contactListView;
    }

    public ContactList getContactList() {
        return contactList;
    }

    public void setContactList(ContactList contactList) {
        this.contactList = contactList;
        if (contactListView == null) {
            contactListView = new ContactListView(this);
            mainPanel.add(contactListView);
        }
        mainPanel.showWidget(1);
    }

    public void onWindowClosed() {
    }

    public String onWindowClosing() {
        listener.onSignOut();
        return null;
    }

    public void onWindowResized(int width, int height) {
        resetChatWindowPositions();
    }

    public ChatWindowView getChatWindowView(Contact contact) {

        ChatWindowView chatWindow = (ChatWindowView) allChatWindows.get(contact.getName());
        if (chatWindow == null) {
            chatWindow = new ChatWindowView(this, contact);
            allChatWindows.put(contact.getName(), chatWindow);
        }
        if (chatWindow.position == -1) {
            chatWindow.position = openChatWindows.size();
            openChatWindows.add(chatWindow);
        }
        chatWindow.show();
        
        return chatWindow;
    }

    public void closeChatWindow(ChatWindowView windowToClose) {
        openChatWindows.remove(windowToClose.position);
        windowToClose.setVisible(false);
        windowToClose.position = -1;
        resetChatWindowPositions();
    }

    protected void resetChatWindowPositions() {
        for (int i = 0; i < openChatWindows.size(); ++i) {
            ChatWindowView chatWindow = (ChatWindowView) openChatWindows.get(i);
            chatWindow.setPosition(i);
        }
    }

    public void onHistoryChanged(String historyToken) {
        Contact contact = contactList.getContact(historyToken);
        if (contact != null) {
            getChatWindowView(contact);
        }
    }

    public AbstractImagePrototype getBoundImage() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
